﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer.DAL;
using DataAccessLayer;
using Common;

namespace BestBuy.ManagerViews
{
    public partial class UserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Trang web lan dau load.
            if (!IsPostBack)
            {
                User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
                if (user == null || user.Status != (int)UserStatus.Active)
                {
                    Response.Redirect("~");
                    return;
                }

                FirstName.Text = user.FirstName;
                LastName.Text = user.LastName;
                Email.InnerText = user.Email;
                PhoneNumber.Text = user.PhoneNumber;
            }
        }

        protected void OldPassMatchValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = UserDAL.ValidateUserLogin(HttpContext.Current.User.Identity.Name, OldPass.Text);
        }

        protected void UpdateUserInfoButton_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.Status != (int)UserStatus.Active)
            {
                Response.Redirect("~");
                return;
            }
            user.FirstName = FirstName.Text.Trim();
            user.LastName = LastName.Text.Trim();
            user.PhoneNumber = PhoneNumber.Text.Trim();

            UserDAL.UpdateUser(user);

            UserInfoMessage.Show("Cập nhật thông tin cá nhân thành công.", UserControls.MessagePanel.MessageType.Success);
        }

        protected void ChangePassButton_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                return;
            }

            UserDAL.UpdatePassword(HttpContext.Current.User.Identity.Name, Password.Text);

            ChangePassMessage.Show("Thay đổi mật khẩu thành công.", UserControls.MessagePanel.MessageType.Success);
        }
        

    }
}